﻿using CPPEI.Coolzon.Designtime;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for ImageSourcesEditWindow.xaml
    /// </summary>
    public partial class ImageSourceFileArrayEditWindow : Window
    {
        public ImageSourceFile[] RetImageSources { get; private set; }
        private ObservableCollection<ImageSourceArrayModel> _ImageSourceList;
        private static ImageRes[] _imageList;
        public ImageSourceFileArrayEditWindow(ImageSourceFile[] imageSources)
        {
            InitializeComponent();
            _imageList = null;
            _ImageSourceList = new ObservableCollection<ImageSourceArrayModel>();
            if (imageSources != null && imageSources.Length > 0)
            {
                foreach(var item in imageSources)
                {
                    ImageSourceArrayModel model = new ImageSourceArrayModel();
                    Uri fullPath = null;
                    if (item != null)
                    {
                        BitmapImage bitmapImage = (BitmapImage)item.ImageSource;
                        fullPath = new Uri(item.Path);
                    }
                    ImageRes selected = null;
                    ImageRes[] imageList = GetImageRes();
                    if (imageList != null && imageList.Length > 0)
                    {
                        foreach (var imageRes in imageList)
                        {
                            if (fullPath != null && fullPath == new Uri(imageRes.FullPath))
                            {
                                selected = imageRes;
                                break;
                            }
                        }
                        if (selected != null)
                        {
                            model.ImageRes = selected;
                        }
                    }
                    _ImageSourceList.Add(model);
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if(_ImageSourceList != null)
            {
                _ctlLV.ItemsSource = _ImageSourceList;
            }
        }

        private void _ctlAdd_Click(object sender, RoutedEventArgs e)
        {
            ImageSourceArrayModel model = new ImageSourceArrayModel();

            _ImageSourceList.Add(model);
        }

        private void _ctlRemove_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlLV.SelectedItem != null && _ctlLV.SelectedIndex >= 0)
            {
                int curIndex = _ctlLV.SelectedIndex;
                ImageSourceArrayModel model = _ctlLV.SelectedItem as ImageSourceArrayModel;
                _ImageSourceList.Remove(model);
            }
        }

        private void _ctlUp_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlLV.SelectedItem != null && _ctlLV.SelectedIndex >= 0)
            {
                int curIndex = _ctlLV.SelectedIndex;
                if (curIndex > 0)
                {
                    ImageSourceArrayModel model = (ImageSourceArrayModel)_ctlLV.SelectedItem;
                    _ImageSourceList.RemoveAt(curIndex);
                    _ImageSourceList.Insert(curIndex - 1, model);

                    _ctlLV.SelectedIndex = curIndex - 1;
                }
            }
        }

        private void _ctlDown_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlLV.SelectedItem != null && _ctlLV.SelectedIndex >= 0)
            {
                int curIndex = _ctlLV.SelectedIndex;
                if (curIndex < _ImageSourceList.Count - 1)
                {
                    ImageSourceArrayModel model = (ImageSourceArrayModel)_ctlLV.SelectedItem;
                    _ImageSourceList.RemoveAt(curIndex);
                    _ImageSourceList.Insert(curIndex + 1, model);

                    _ctlLV.SelectedIndex = curIndex + 1;
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RetImageSources = new ImageSourceFile[_ImageSourceList.Count];
            int index = 0;
            foreach (var item in _ImageSourceList)
            {
                if (item.ImageSource == null)
                {
                    CustomMessageBox.ShowWarning("请选择图片");
                    return;
                }
                RetImageSources[index++] = new ImageSourceFile(item.ImageSource, item.ImageRes.FullPath);
            }
            DialogResult = true;
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        public static ImageRes[] GetImageRes()
        {
            if(_imageList == null)
            {
                _imageList = ResPoolFactory.GetImageResPool().GetImages();
            }
            return _imageList;
        }

        private void Element_GotFocus(object sender, RoutedEventArgs e)
        {
            var aa = (sender as FrameworkElement).DataContext;
            _ctlLV.SelectedItem = aa;
        }
    }
}
